// CViewSub.cpp : implementation file
//

#include "stdafx.h"
#include "PanImage.h"
#include "ViewSub.h"

#include "MainFrm.h"

#include "DD_AnalyCfg.h"
#include "DDErrorDefine.h"
#include "MyDefineHeader.h"

/////////////////////////////////////////////////////////////////////////////
// CViewSub

IMPLEMENT_DYNCREATE(CViewSub, CMyFormView)

CViewSub::CViewSub()
	: CMyFormView()
{
	//{{AFX_DATA_INIT(CViewSub)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_nDelDefectS  = 0;
	m_bClearDefect = FALSE;
	m_bStartClear  = FALSE;
}

CViewSub::~CViewSub()
{
}

void CViewSub::DoDataExchange(CDataExchange* pDX)
{
	CMyFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CViewSub)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CViewSub, CMyFormView)
	//{{AFX_MSG_MAP(CViewSub)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewSub diagnostics

#ifdef _DEBUG
void CViewSub::AssertValid() const
{
	CMyFormView::AssertValid();
}

void CViewSub::Dump(CDumpContext& dc) const
{
	CMyFormView::Dump(dc);
}
#endif //_DEBUG

void CViewSub::Initialize()
{
	m_pRuler = &(g.MainWnd->m_AnalyRulers[eAnalyWindow_Sub]);
    s.ImgAnaly.SetHwnd( eAnalyWindow_Sub, m_hWnd );
	m_bInit = true ;
}



/////////////////////////////////////////////////////////////////////////////
// CViewSub message handlers

void CViewSub::OnDraw(CDC* /*pDC*/)
{
	// TODO: Add your specialized code here and/or call the base class
	if( !m_bInit )
	{
		return;
	}

	DDOnDraw();
}

void CViewSub::DDOnDraw()
{
	POINT tScrollPos      = (POINT)GetScrollPosition();

	zkImgDraw xImgDraw;
	xImgDraw.nScrollx    = tScrollPos.x;
	xImgDraw.nScrolly    = tScrollPos.y;
	xImgDraw.fZoomFactor = m_fZoomFactor;

	s.ImgAnaly.Draw(eAnalyWindow_Sub,&xImgDraw);
}




int CViewSub::CB_OnDrawMarkFun(HDC hDc,int nWidth,int nHeight,DWORD dwDispType,void *pContext)
{
	CViewSub *pView = (CViewSub*)pContext;
	return pView->DXOnDraw(hDc,nWidth,nHeight,dwDispType);
}



void CViewSub::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CPoint Pt = point;
	GetImageXY(Pt);

	if( IsInside( Pt.x,Pt.y ) )
	{
		DelDefect( &Pt );
	}

	CMyFormView::OnMouseMove(nFlags, point);
}
void CViewSub::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CPoint Pt = point;
	GetImageXY( Pt );
	if( IsInside( Pt.x,Pt.y ) )
	{
		if( m_bClearDefect )
		{
			m_bStartClear = TRUE;
		}
		DelDefect( &Pt );
	}

	SetCapture(); 

	CMyFormView::OnLButtonDown(nFlags, point);
}

void CViewSub::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CPoint Pt = point;
	GetImageXY(Pt);

	if( IsInside( Pt.x,Pt.y ) )
	{	
		DelDefect( &Pt );

		m_bStartClear = FALSE;
	}


	ReleaseCapture();

	CMyFormView::OnLButtonUp(nFlags, point);
}

void CViewSub::DelDefect(CPoint *pPt)
{
    if( !m_bStartClear ) return;
	if( m_nDelDefectS <= 0 ) return;

	m_xDefect.x     = pPt->x;
	m_xDefect.y     = pPt->y;
	m_xDefect.nSize = m_nDelDefectS;

	s.ImgAnaly.OperateMethod(eAnalyMethod_DelSubDefect,&m_xDefect);

	DDOnDraw();
}

void CViewSub::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	g.MainWnd->PostMessage(WM_USER_ZoomAnalyView,eAnalyWindow_Sub,0);
	CMyFormView::OnLButtonDblClk(nFlags, point);
}
void CViewSub::ZoomOut()
{
	m_fZoomFactor *= MouseWheelZoomRate;
	InvalidateView();
	g.ViewmAnaly[eAnalyWindow_Ref]->InvalidateView(m_fZoomFactor);
	g.ViewmAnaly[eAnalyWindow_Sap]->InvalidateView(m_fZoomFactor);
	g.ViewmAnaly[eAnalyWindow_Adj]->InvalidateView(m_fZoomFactor);
}

void CViewSub::ZoomIn()
{
	m_fZoomFactor /= MouseWheelZoomRate;
	InvalidateView();
	g.ViewmAnaly[eAnalyWindow_Ref]->InvalidateView(m_fZoomFactor);
	g.ViewmAnaly[eAnalyWindow_Sap]->InvalidateView(m_fZoomFactor);
	g.ViewmAnaly[eAnalyWindow_Adj]->InvalidateView(m_fZoomFactor);
}

int CViewSub::DXOnDraw(HDC hDc,int nWidth,int nHeight,DWORD dwDispType)
{
	if( m_bStartClear )
	{
		HPEN  hpen     = NULL;
		HPEN  old_hpen = NULL;

		hpen     = CreatePen(PS_SOLID,1,RGB(200,200,30)); 
		old_hpen = (HPEN)SelectObject(hDc,hpen);

		zkAnalyClearArea *pDefect = &m_xDefect;
		int nLeft   = pDefect->x - pDefect->nSize/2;
		int nRight  = nLeft + pDefect->nSize;
		int nTop    = pDefect->y - pDefect->nSize/2;
		int nBottom = nTop + pDefect->nSize;

		MoveToEx(hDc,nLeft,nTop,NULL);
		LineTo(hDc,nLeft,nBottom);
		LineTo(hDc,nRight,nBottom);
		LineTo(hDc,nRight,nTop);
		LineTo(hDc,nLeft,nTop);

		SelectObject(hDc,old_hpen); 
		DeleteObject(hpen); 
	}

	return 0;
}
